<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends Base_Controller {

    public function index() {
        $this->load->view('public/header', $this->data);
        $this->load->view('pages/home');
    }

    public function logout() {
        $this->session->sess_destroy();
        redirect('user/login');
        die();
    }

    public function config() {
        if ($this->session->level == 99) {
            $this->data['sendconfig'] = $this->common_model->getList('su_sendname');
        } else {
            $this->data['sendconfig'] = $this->common_model->getList('su_sendname', ['department_id' => $this->session->department_id]);
        }
        $this->load->view('public/header', $this->data);
        $this->load->view('pages/config');
    }

    public function profile() {
        $this->load->view('public/header', $this->data);
        $this->load->view('pages/profile');
    }

    public function add_department() {
        $redata = $this->httpcode;
        $name = $this->input->post('department');
        $table = 'su_department';
        $data = [
            'department_name' => trim($name),
            'create_time' => tnow()
        ];


        if ($department_id = $this->config_model->addConfig($table, $data)) {
            $senddata = [
                'send_name' => $name,
                'department_id' => $department_id,
                'department_name' => $name
            ];
            $this->common_model->addItem('su_sendname', $senddata);
            $redata['errno'] = '666';
            $redata['error'] = '添加部门成功';
        } else {
            $redata['errno'] = '600';
            $redata['error'] = '添加部门失败';
        }
        echo json_encode($redata);
    }

    public function get_department() {
        $redata = $this->httpcode;
        if ($this->session->level >= 99) {
            $table = 'su_department';
            $departmentlist = $this->config_model->getConfig($table);
            if (!empty($departmentlist)) {
                $redata['errno'] = '666';
                $redata['error'] = '获取部门列表成功';
                $redata['departmentlist'] = $departmentlist;
            } else {
                $redata['errno'] = '600';
                $redata['error'] = '部门列表为空';
            }
        } else {
            $redata['errno'] = '600';
            $redata['error'] = '非法请求';
        }

        echo json_encode($redata);
    }

    public function update_config() {
        $redata = $this->httpcode;
        $config = $this->input->post('config');

        if ($this->session->level >= 99) {
            $table = 'su_config';
            foreach ($config as $k => $v) {
                $where = [
                    'name' => $k
                ];
                $data = [
                    'value' => $v
                ];
                $this->config_model->updateConfig($table, $where, $data);
            }
            $redata['errno'] = '666';
            $redata['error'] = '配置修改成功';
        } else {
            $redata['errno'] = '600';
            $redata['error'] = '非法请求';
        }

        echo json_encode($redata);
    }

    public function update_sendname() {
        $redata = $this->httpcode;
        $sendname = $this->input->post('sendname');
        if ($this->session->level == 50) {
            $table = 'su_sendname';
            $where = [
                'department_id' => $this->session->department_id
            ];
            $data = [
                'send_name' => $sendname
            ];
            $this->config_model->updateConfig($table, $where, $data);
            $redata['errno'] = '666';
            $redata['error'] = '昵称修改成功';
        } else {
            $redata['errno'] = '600';
            $redata['error'] = '非法请求';
        }

        echo json_encode($redata);
    }

    //邮件服务器管理
    public function mailserver() {
        $this->load->view('public/header', $this->data);
        $this->load->view('pages/mailserver');
    }

    public function get_mailserver() {
        $redata = $this->httpcode;
        if ($this->session->level >= 0) {
            $table = 'su_mailserver';
            $mailserverlist = $this->common_model->getList($table);
            $query = $this->db->select('su_mailserver.*,su_department.id as tid,su_department.department_name')->join('su_department', 'su_department.id=su_mailserver.department_id', 'left')->get('su_mailserver');
            $mailserverlist = $query->result_array();

            if (!empty($mailserverlist)) {
                $redata['errno'] = '666';
                $redata['error'] = '获取邮件服务器列表成功';
                $redata['Mailserverlist'] = $mailserverlist;
            } else {
                $redata['errno'] = '600';
                $redata['error'] = '邮件服务器列表为空';
            }
        } else {
            $redata['errno'] = '600';
            $redata['error'] = '非法请求';
        }

        echo json_encode($redata);
    }

    public function add_mailserver() {
        $redata = $this->httpcode;
        $department_id = $this->input->post('department_id');
        $server_address = $this->input->post('server_address');
        $server_host = $this->input->post('server_host');
        $server_sender = $this->input->post('server_sender');
        $data = [
            'department_id' => $department_id,
            'server_address' => $server_address,
            'server_host' => $server_host,
            'server_sender' => $server_sender,
            'create_time' => tnow()
        ];
        if ($this->session->level >= 99) {
            $table = 'su_mailserver';
            $state = $this->common_model->addItem($table, $data);
            if ($state) {
                $redata['errno'] = '666';
                $redata['error'] = '添加邮件服务器成功';
            } else {
                $redata['errno'] = '600';
                $redata['error'] = '添加邮件服务器失败';
            }
        } else {
            $redata['errno'] = '600';
            $redata['error'] = '非法请求';
        }

        echo json_encode($redata);
    }

    public function del_mailserver() {
        $redata = $this->httpcode;
        $id = $this->input->post('id');
        $where = [
            'id' => $id,
        ];
        if ($this->session->level >= 99) {
            $table = 'su_mailserver';
            $state = $this->common_model->delItem($table, $where);
            if ($state) {
                $redata['errno'] = '666';
                $redata['error'] = '删除邮件服务器成功';
            } else {
                $redata['errno'] = '600';
                $redata['error'] = '删除邮件服务器失败';
            }
        } else {
            $redata['errno'] = '600';
            $redata['error'] = '非法请求';
        }

        echo json_encode($redata);
    }

    public function update_mailserver() {
        $redata = $this->httpcode;
        $id = $this->input->post('id');
        $server_address = $this->input->post('server_address');
        $server_host = $this->input->post('server_host');
        $server_sender = $this->input->post('server_sender');
        $where = [
            'id' => $id,
        ];
        $data = [
            'server_address' => $server_address,
            'server_host' => $server_host,
            'server_sender' => $server_sender,
            'update_time' => tnow()
        ];
        if ($this->session->level >= 99) {
            $table = 'su_mailserver';
            $state = $this->common_model->updateItem($table, $where, $data);
            if ($state) {
                $redata['errno'] = '666';
                $redata['error'] = '更新邮件服务器成功';
            } else {
                $redata['errno'] = '600';
                $redata['error'] = '更新邮件服务器失败';
            }
        } else {
            $redata['errno'] = '600';
            $redata['error'] = '非法请求';
        }

        echo json_encode($redata);
    }

    public function handinit() {
        $redata = $this->httpcode;
        $where = [
            'department_id' => $this->session->department_id
        ];
        $initdata_user = [
            'today_usage' => 0
        ];
        $data = [
            'today_usage' => 0,
            'enable' => 1
        ];
        if ($this->session->level >= 50) {
            $state_user = $this->common_model->updateItem('su_user', $where, $initdata_user);
            $state_sender = $this->common_model->updateItem('su_sender', $where, $data);
            $state_receiver = $this->common_model->updateItem('su_receiver', $where, $data);
            $state_mailserver = $this->common_model->updateItem('su_mailserver', $where, $data);
            if ($state_user && $state_sender && $state_receiver && $state_mailserver) {
                $redata['errno'] = '666';
                $redata['error'] = '数据重置成功';
            } else {
                $redata['errno'] = '600';
                $redata['error'] = '数据重置失败';
            }
        } else {
            $redata['errno'] = '601';
            $redata['error'] = '无权操作';
        }

        echo json_encode($redata);
    }

}
